package com.project.util;

import java.math.BigDecimal;
import java.util.Date;

public class Util {

    public static String getTemplateString(String name) {
        return "\\$\\{%\\^" + name + "\\^%}";
    }

    // 将表名处理
    // 下划线转驼峰命名法
    // 首字母大写
    public static String mysqlTableNameToJavaName(String tableName) {
        StringBuilder sb = new StringBuilder(tableName);
        Integer i = sb.indexOf("_");
        while (i != -1) {
            if ( i == 0 ){
                sb.delete(0,1);
            }else if ( i == sb.length()-1 ){
                sb.delete(sb.length()-1,sb.length());
            }
            sb.delete(i, i+1);
            sb.setCharAt(i, charToBigLetter(sb.charAt(i)));
            i = sb.indexOf("_");
        }
        return sb.toString();
    }

    public static char charToBigLetter(char charAt) {
        if ( charAt >= 'a' && charAt <= 'z' )
            return (char) (charAt-32);
        return charAt;
    }


    public static String firstCharBigLetter(String s) {
        StringBuilder sb = new StringBuilder(s);
        sb.setCharAt(0, charToBigLetter(sb.charAt(0)));
        return sb.toString();
    }

    public static Class mySqlTypeToJavaType(String mySqlType) {
        Class javaType = null;
        switch (mySqlType) {
            case "CHAR":
                javaType = Character.class;
                break;
            case "VARCHAR":
                javaType = String.class;
                break;
            case "BLOB":
                javaType = Byte[].class;
                break;
            case "TEXT":
                javaType = String.class;
                break;
            case "INT":
                javaType = Integer.class;
                break;
            case "SMALLINT":
                javaType = Integer.class;
                break;
            case "TINYINT":
                javaType = Integer.class;
                break;
            case "MEDIUMINT":
                javaType = Integer.class;
                break;
            case "BIT":
                javaType = Boolean.class;
                break;
            case "BIGINT":
                javaType = Long.class;
                break;
            case "FLOAT":
                javaType = Float.class;
                break;
            case "DOUBLE":
                javaType = Double.class;
                break;
            case "DECIMAL":
                javaType = BigDecimal.class;
                break;
            case "DATE":
                javaType = Date.class;
                break;
            case "TIME":
                javaType = Date.class;
                break;
            case "DATETIME":
                javaType = Date.class;
                break;
            case "TIMESTAMP":
                javaType = Date.class;
                break;
            default:
                throw new RuntimeException("亲,你要转换的mysql类型[" + mySqlType + "]不存在!");
        }
        return javaType;
    }
}
